﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;
using GPM;

namespace LF3Proj
{
    public partial class FrmEditUserInfo : BaseEditForm
    {
        EditUserInfoController controller = new EditUserInfoController();
        private string UserID = "";
        public FrmEditUserInfo()
        {
            InitializeComponent();
        }

        public FrmEditUserInfo(string id, string userid)
            : this()
        {
            base.EntityID = id;
            this.UserID = userid;
        }

        /// <summary>
        /// 编辑或者保存状态下取值函数
        /// </summary>
        /// <param name="info"></param>
        private void SetInfo(User_Info info)
        {
            info.user_id = this.txtUserID.Text;
            info.user_name = this.txtUserName.Text;
            info.password = this.txtPassword.Text;
            info.password = this.txtPasswordConfirm.Text;
            //info.dept_id = Convert.ToInt32(this.lookupDeptID.EditValue);
            //info.group_id = Convert.ToInt32(this.lookupGroupID.EditValue);
            info.is_enabled = this.chkEditEnabled.Checked == true ? "1" : "0";
            info.remark = this.memoRemark.Text;
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            this.lookupDeptID.Properties.DataSource = controller.GetAllDepts();
            this.lookupGroupID.Properties.DataSource = controller.GetAllGroups();
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtUserID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：用户ID不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：用户真实姓名不能为空！", AppSetter.SoftName);
                return false;
            }

            //if (this.lookupDeptID.EditValue == null)
            //{
            //    ExMessageBox.Show("提示信息：部门不能为空！", AppSetter.SoftName);
            //    return false;
            //}

            //if (this.lookupGroupID.EditValue == null)
            //{
            //    ExMessageBox.Show("提示信息：默认角色不能为空！", AppSetter.SoftName);
            //    return false;
            //}

            
            return true;
        }

        public override void DisplayData()
        {
            //InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                User_Info userinfo = controller.GetUserInfo(this.UserID);
                #region 显示用户信息
                this.txtUserName.Focus();
                if (null != userinfo)
                {
                    this.txtUserID.Text = userinfo.user_id;
                    this.txtUserID.Enabled = false;
                    this.txtUserName.Text = userinfo.user_name;
                    //this.txtPassword.Enabled = false;
                    //this.txtPasswordConfirm.Enabled = false;
                    //this.lookupDeptID.EditValue = userinfo.dept_id;
                    //this.lookupGroupID.EditValue = userinfo.group_id;
                    this.chkEditEnabled.Checked = userinfo.is_enabled == "1" ? true : false;
                    this.memoRemark.Text = userinfo.remark;
                }

                #endregion
            }
            else
            {
                this.txtUserID.Focus();
            }
        }

        public override bool SaveAddNew()
        {
            if (string.IsNullOrEmpty(this.txtPassword.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：密码不能为空！", AppSetter.SoftName);
                return false;
            }

            if (this.txtPassword.Text.Trim() != this.txtPasswordConfirm.Text.Trim())
            {
                ExMessageBox.Show("提示信息：两次密码输入不一致！", AppSetter.SoftName);
                return false;
            }
             
            User_Info info = new User_Info();
            this.SetInfo(info);
            controller.AddUser(info);
            LogHelper.Log("用户管理", "新增", "", "user:" +
               info.user_id.ToString());
            UserInfoController uc = new UserInfoController(this.Owner.ActiveMdiChild as FrmUserInfo);
            uc.QueryUserInfo();
            return true;
        }

        public override bool SaveUpdated()
        {
            User_Info info = new User_Info();
            this.SetInfo(info);
            controller.EditUser(info);
            LogHelper.Log("用户管理", "编辑", "", "user:" +
                info.user_id.ToString());
            UserInfoController uc = new UserInfoController(this.Owner.ActiveMdiChild as FrmUserInfo);
            uc.QueryUserInfo();
            return true;
        }

    }
}
